﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Text.RegularExpressions;
using SuperSocialShare.Clients;
using SuperSocialShare.Authorize;
using SuperSocialShare.Helper;

namespace SuperSocialShare.Controls
{
    public partial class AuthorizeControl : UserControl
    {
        public delegate void AuthorizeCompletedEventHandler(AsyncEventArgs e);
        public event AuthorizeCompletedEventHandler AuthorizeCompleted;

        private bool _flag = false;


        public AuthorizeControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 获取或设置获取授权的示例
        /// </summary>
        public SocialClient Client { set; get; }

        public void BeginRequest()
        {
            Uri uri = new Uri(Client.GetRequestAuthorizeUrl());
            Client.GetAuthorizeInfoCompleted += Client_GetAuthorizeInfoCompleted;
            WebBrowser.Navigate(uri);
        }

        private void WebBrowser_Navigating(object sender, NavigatingEventArgs e)
        {
            indicator.Visibility = Visibility.Visible;
            String uri = e.Uri.AbsoluteUri;
            if (!_flag)
            {
                switch (Client.SocialType)
                {
                    case SocialType.Sina://新浪api只提供Authorization Code方式
                    case SocialType.Renren:
                    case SocialType.Tencent:
                        if (uri.Contains("?code="))
                        {
                            Client.ProcessResponseDataOfAuthorize(uri);
                            _flag = true;
                        }
                        break;
                    case SocialType.KaiXin://以Implicit Grant方式获取授权信息
                    case SocialType.SoHu:
                    case SocialType.NetEase:
                        if (uri.Contains("access_token="))
                        {
                            Client.ProcessResponseDataOfAuthorize(uri);
                            _flag = true;
                        }
                        break;
                    default:
                        break;
                }
            }

        }

        void Client_GetAuthorizeInfoCompleted(object sender, AsyncEventArgs e)
        {
            if (e.Error == null)
            {
                AuthorizeInfo info = e.Result as AuthorizeInfo;
                AuthorizeManager.Save(info, Client.SocialType);//保存授权信息
                AsyncEventArgs args = new AsyncEventArgs();
                args.Result = info;
                if (AuthorizeCompleted != null)
                {
                    AuthorizeCompleted(args);
                }
            }

        }

        private void WebBrowser_LoadCompleted(object sender, NavigationEventArgs e)
        {
            indicator.Visibility = Visibility.Collapsed;
        }

    }
}
